<?php
/*
 * Forescore
 * A golf scoring and analysis app
 * 
 * @license   MIT License
 * @package   Forescore
 * @author    $Author: brian@brianvoss.com $
 * @version   $Id: Base.php 12 2013-06-28 03:28:14Z brian@brianvoss.com $
 * @lastrevision   $Date: 2013-06-28 03:28:14 +0000 (Fri, 28 Jun 2013) $
 * @filesource     $URL: http://forescore.googlecode.com/svn/trunk/library/Forescore/Model/Base.php $
 */

class Forescore_Model_Base {
	/**
	 * Constructor
	 * @param [array $data]
	 */
	public function __construct(array $data = null)
	{
		if (is_array($data))
		{
			$this->setData($data);
		}
	}

	/**
	 * Catch-all setter method
	 * @param string $name
	 * @param mixed $value
	 */
	public function __set($name, $value)
	{
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method))
		{
			throw new Exception('Invalid Model property');
		}
		$this->$method($value);
	}

	/**
	 * Catch-all getter method
	 * @param string $name
	 * @param mixed $value
	 */
	public function __get($name)
	{
		$method = 'get' . $name;
		if (('mapper' == $name) || !method_exists($this, $method))
		{
			throw new Exception('Invalid Model property');
		}
		return $this->$method();
	}

	/**
	 * Batch setter method
	 * @param array $data
	 */
	public function setData(array $data)
	{
		$methods = get_class_methods($this);
		foreach ($data as $key => $value)
		{
			// If data key name has underscores in it
			// transform the key name to camel case
			if (stristr($key, "_"))
			{
				$filter = new Zend_Filter_Word_UnderscoreToCamelCase();
				$key = $filter->filter($key);
			}
			$method = 'set' . ucfirst($key);
			if (in_array($method, $methods))
			{
				$this->$method($value);
			}
		}
		return $this;
	}
}

?>
